import { RuleObject } from 'antd/lib/form';
import { trim, len } from '../../utils/string';
// import { RuleObject } from 'antd/lib/form'
interface IValidate {
    // (message: string, ...args: any[]): {
    //     validator(rule: RuleObject, value: any, callback: (msg: string|void) => void): void | Promise<void>
    // }
    (message: string, ...args: any[]): any;
}
export namespace Validator {
    export const MaxLength: IValidate  = (message: string, maxLen: number) => {
        return {
            validator: (rule: RuleObject, value: any) => {
                if(len(trim(value)) > maxLen) {
                    return Promise.reject(message)
                } else {
                    return Promise.resolve()
                }
            }
        }
    }
    export const MinLength: IValidate = (message: string, minLen: number) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(len(trim(value)) < minLen) {
                    return Promise.reject(message)
                } else {
                    return Promise.resolve()
                }
            }
        }
    }

    export const Max: IValidate = (message: string, val: number) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(val > val) {
                    callback(message)
                } else {
                    callback();
                }
            }
        }
    }

    export const Min: IValidate = (message: string, val: number) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(val < val) {
                    callback(message)
                } else {
                    callback();
                }
            }
        }
    }

    export const IsPhone: IValidate = (message: string) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(!(/^1[3456789]d{9}$/.test(trim(value)))){ 
                    callback(message)
                }  else {
                    callback();
                }
            }
        }
    }

    const regEmail = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$"); 
    export const IsEmail: IValidate = (message: string) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(!regEmail.test(trim(value))){ 
                    callback(message)
                }  else {
                    callback();
                }
            }
        }
    }

    const specialReg = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/im; 
    export const IsSpecialChar: IValidate = (message: string) => {
        return {
            validator(rule: RuleObject, value: any, callback: (msg: string|void) => void) {
                if(!specialReg.test(trim(value))){ 
                    callback(message)
                }  else {
                    callback();
                }
            }
        }
    }
}